package cn.Alibaba.system.controller;

import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.system.domain.Dictionarydetail;
import cn.Alibaba.system.query.DictionarydetailQuery;
import cn.Alibaba.system.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：字典细节
 */
@RestController
@RequestMapping("/dictionarydetail")
@Api(value = "字典细节的API",description="字典细节相关的CRUD功能") //打在Controller对Controller做描述
public class DictionarydetailController {

    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}") // http://localhost:8080/dictionarydetail/1 + get
    public Dictionarydetail findOne(@PathVariable("id") Long id){
        return dictionarydetailService.findOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有" )// 打在方法上，对方法做描述
    @GetMapping // http://localhost:8080/dictionarydetail + get  与上面的请求参数不同所以请求方式是不一样的
    public List<Dictionarydetail> findAll(){

        return dictionarydetailService.findAll();
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作" )// 打在方法上，对方法做描述
    @DeleteMapping("/{id}") // http://localhost:8080/dictionarydetail + delete
    public JsonResult del(@PathVariable("id") Long id){
        try {
            dictionarydetailService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加或修改
     * @param dictionarydetail
     * @return
     */
    @ApiOperation(value = "添加或修改" )// 打在方法上，对方法做描述
    @PutMapping // http://localhost:8080/dictionarydetail +JSon对象 + delete
    public JsonResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){ // 从请求体中获取JSON对象

        try {
            if (dictionarydetail.getId()==null){
                dictionarydetailService.save(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }

            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询" )// 打在方法上，对方法做描述
    @PostMapping// http://localhost:8080/dictionarydetail +JSon对象 + post
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery query){
        return dictionarydetailService.queryPage(query);
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @ApiOperation(value = "批量删除操作" )// 打在方法上，对方法做描述
    @PatchMapping
    // 可以用数组 也可以用集合List<Long>
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            dictionarydetailService.PatchDel( ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
